/**
 * 时间：2018-9-23
 * 功能：Android 各种 UI 控件的使用
 */
package com.example.zht.theuseofcontrols;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_image;
    private ImageView image_view;
    private Button btn_progressbar;
    private ProgressBar progressBar;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionbar = getSupportActionBar();
        if (actionbar != null) {
            actionbar.hide();
        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setTitle("ProgressDialog");
        progressDialog.setMessage("loading......");
        progressDialog.setCancelable(true);
        progressDialog.show();
        btn_image = findViewById(R.id.btn_image);
        image_view = findViewById(R.id.image_View);
        btn_progressbar = findViewById(R.id.btn_progressbar);
        progressBar = findViewById(R.id.progressBar);
        btn_image.setOnClickListener(this);
        btn_progressbar.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_image:
                image_view.setImageResource(R.drawable.img2);
                break;
            case R.id.btn_progressbar:
                // 将圆形进度条显示与否通过点击按钮来实现，若已显示，则点击按钮进行隐藏，否则显示进度条
//                if (progressBar.getVisibility() == View.GONE) {
//                    progressBar.setVisibility(View.VISIBLE);
//                } else {
//                    progressBar.setVisibility(View.GONE);
//                }
                // 方形进度条，通过点击按钮对进度条进度进行累加，最大值为 100
                int progress = progressBar.getProgress();
                progress += 10;
                progressBar.setProgress(progress);
                if (progress == 100) {
                    AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
                    alertDialog.setTitle("Progress Complete!");
                    alertDialog.setMessage("progress load is completed!!!");
                    alertDialog.setCancelable(false);
                    alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(MainActivity.this, "you clicked OK!", Toast.LENGTH_SHORT).show();
                        }
                    });
                    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(MainActivity.this, "you clicked Cancel!", Toast.LENGTH_SHORT).show();
                        }
                    });
                    alertDialog.show();
                    progress = 0;
                    progressBar.setProgress(progress);
                }
                break;
        }
    }
}
